<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\utils\PrinterUtil;
use think\Db;
use think\Config;

/**
 * 打印机管理接口
 */
class Printer extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function printOrder()
    {
        $merOrderId     =   $this->request->post('merOrderId', false);
        if(!$merOrderId){
            $this->error('订单号不能为空', null, 0);
        }

        // 查询订单信息
        $orderInfo = Db::name('payment')->where('order_no', $merOrderId)->find();
        if(!$orderInfo){
            $this->error('订单不存在', null, 0);
        }

        // 打印内容
        // print_r($orderInfo);
        $printerUtil = new PrinterUtil();
        $content = Config::get('site')['PRINT_TEMPLATE'];

        $replace_arr = [
            'title'     =>  '供奉佛灯测试',
            'name'      =>  $orderInfo['username'],
            'phone'     =>  substr_replace($orderInfo['phone'], '****', 3, 4),
            'buddha'    =>  $orderInfo['buddha_name'],
            'buddha_no'  =>  $orderInfo['buddha_no'],
            'blessing'  =>  $orderInfo['blessing'],
            'timespan'  =>  $orderInfo['timespan'],
            'money'     =>  '￥' . $orderInfo['money'],
            'time'      =>  date('Y-m-d H:i:s', $orderInfo['create_time']),
            'print_time'=>  date('Y-m-d H:i:s', time()),
        ];

        foreach($replace_arr as $k=>$v){
            $content    =   str_replace('{' . $k . '}', $v, $content);
        }

        // print_r($content);
        // print_r($this->printerUtil->printMsg(Config::get('site')['SNCODE'], $content));
        // 订单记录打印状态, 1:已打印
        Db::name('payment')->where('order_no', $merOrderId)->update(['print_status'=>1]);
        $this->success('打印成功', null, 0);
    }
}
